import 'package:enter_prise_web/util/MyDio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_calendar_carousel/classes/event.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Calendar extends StatefulWidget {
  const Calendar({super.key, required this.uid, required this.name});
  final String uid;
  final String name;

  @override
  State<Calendar> createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
  var _targetDateTime = DateTime.now();
  var _date2 = DateTime.now();
  final myDio = MyDio();

  var times = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // var response =
    //     myDio.getData("/attendance/get", params: {"uid": widget.uid});
    // response.then((value) => setState(() => times = value.data['data']));
  }

  final EventList<Event> _markedDateMap = EventList<Event>(
    events: {
      DateTime(2024, 1, 22): [
        Event(
          date: DateTime(2024, 1, 22),
        ),
      ],
      DateTime(2024, 1, 23): [
        Event(
          date: DateTime(2024, 1, 23),
        ),
      ],
      DateTime(2023, 12, 23): [
        Event(
          date: DateTime(2024, 1, 23),
        ),
      ],
    },
  );
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('${widget.name}到勤情况'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          Container(
            padding: const EdgeInsets.all(30.0),
            child: Row(
              children: [
                Text(
                  "${_targetDateTime.year}年 ${_targetDateTime.month}月",
                  style: const TextStyle(
                      fontSize: 35, fontWeight: FontWeight.bold),
                ),
                const Spacer(),
                Container(
                  width: 120,
                  height: 60,
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: TextButton(
                      child: const Text(
                        "PREV",
                        style: TextStyle(fontSize: 25, color: Colors.black),
                      ),
                      onPressed: () {
                        setState(() {
                          _targetDateTime = DateTime(
                              _targetDateTime.year, _targetDateTime.month - 1);
                        });
                      }),
                ),
                // e9e9db
                Container(
                  width: 120,
                  height: 60,
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(5)),
                  child: TextButton(
                      child: const Text(
                        "NEXT",
                        style: TextStyle(fontSize: 25, color: Colors.black),
                      ),
                      onPressed: () {
                        _targetDateTime = DateTime(
                            _targetDateTime.year, _targetDateTime.month + 1);
                        if (_targetDateTime.isAfter(DateTime.now())) {
                          _targetDateTime = DateTime(
                              _targetDateTime.year, _targetDateTime.month - 1);
                          Fluttertoast.showToast(msg: "还没有到那一月呢，你想看啥？");
                        }
                        setState(() {});
                      }),
                ),
              ],
            ),
          ),
          CalendarCarousel(
            // 日期的点击事件
            selectedDateTime: _date2,
            selectedDayButtonColor: Colors.orange,
            selectedDayBorderColor: Colors.transparent,
            targetDateTime: _targetDateTime,
            onDayPressed: (DateTime date, List events) {
              // date点击的日期
              setState(() {
                _date2 = date;
              });
            },
            // 标记日期
            todayBorderColor: Colors.green,
            todayTextStyle: const TextStyle(fontSize: 20),
            todayButtonColor: Colors.green,
            // 最大只能点今天之前
            maxSelectedDate: DateTime.now(),

            // 语言
            locale: 'zh',
            // 月份发生改变的时候
            onCalendarChanged: (e) {
              // 月份切换的时候可以设置默认标记的日期dateTimeList，就能动态设置标记日期
            },
            showHeader: false,
            customDayBuilder: (
              bool isSelectable,
              int index,
              bool isSelectedDay,
              bool isToday,
              bool isPrevMonthDay,
              TextStyle textStyle,
              bool isNextMonthDay,
              bool isThisMonthDay,
              DateTime day,
            ) {
              return null;
              // 默认标记
              // 这里的dateTimeList是一个List<DateTime>类型的数组，contains是判断数组中是否包含day，如果包含就自定义样式
              // 这里的day是这个月的每一天，这个插件会自动循环每一天，然后我们来判断当前的那一天在不在默认标记中
            },
            firstDayOfWeek: 0,
            markedDatesMap: _markedDateMap,
            markedDateWidget: const Text("已签"),
            prevDaysTextStyle:
                const TextStyle(fontSize: 20, color: Color(0xfffea157)),
            daysTextStyle: const TextStyle(fontSize: 20, color: Colors.black),
            nextDaysTextStyle:
                const TextStyle(fontSize: 20, color: Colors.green),
            weekendTextStyle: const TextStyle(
              color: Colors.black,
              fontSize: 20,
            ), // 周六周天颜色
            weekdayTextStyle: const TextStyle(color: Colors.green),
            height: 500.0, // 高度
            daysHaveCircularBorder: true,
          ),
        ],
      ),
    );
  }
}
